package juc.lock;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fangbin
 * @desc  避免死锁
 * @date 2020/5/25 7:18
 */
public class TryLockDeadLock  implements Runnable{
    int flag=1;
    static Lock lock1 = new ReentrantLock();
    static Lock lock2 = new ReentrantLock();

    public TryLockDeadLock(int flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        for(int i=0;i<100;i++) {
            if (flag == 1) {
                try {
                    if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程一获取到了锁1");
                            Thread.sleep(new Random().nextInt(1000));
                            if (lock2.tryLock(800, TimeUnit.MILLISECONDS)) {
                                try {
                                    System.out.println("线程一获取到了锁2");
                                    System.out.println("线程一成功获取到两把锁");
                                    break;
                                } finally {
                                    lock2.unlock();
                                    Thread.sleep(new Random().nextInt(1000));
                                }
                            }else {
                                System.out.println("线程一获取锁2失败");
                            }
                        } finally {
                            lock1.unlock();
                        }
                    }else {
                        System.out.println("线程一获取锁1失败");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (flag == 2) {
                try {
                    if (lock2.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程二获取到了锁2");
                            Thread.sleep(new Random().nextInt(1000));
                            if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
                                try {
                                    System.out.println("线程二获取到了锁1");
                                    System.out.println("线程二成功获取到两把锁");
                                    break;
                                } finally {
                                    lock1.unlock();
                                }
                            }else {
                                System.out.println("线程二获取锁1失败");
                            }
                        } finally {
                            lock2.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    }else {
                        System.out.println("线程二获取锁2失败");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public static void main(String[] args) {
        new Thread(new TryLockDeadLock(1)).start();
        new Thread(new TryLockDeadLock(2)).start();
    }
}
